package com.alog.storage.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.alog.storage.Contants;
import com.alog.storage.R;

/**
 * 数据为空、服务器错误、无网络时显示的控件
 */
public class NodataLayout extends LinearLayout {
    public static final int TYPE_NO_NETWORK = 1;
    public static final int TYPE_SERVER_ERROR = 2;
    public static final int TYPE_NO_DATA = 3;
    private TextView mDescTv;
    private ImageView mIconIv;
    private Button mRefreshBtn;
    private SwipeRefreshLayout.OnRefreshListener mListener;
    private int mType;

    public NodataLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.nodata, this);
        init();
    }

    private void init() {
        mDescTv = (TextView) findViewById(R.id.tv_nodata_desc);
        mIconIv = (ImageView) findViewById(R.id.iv_nodata_icon);
        mRefreshBtn = (Button) findViewById(R.id.btn_nodata_refresh);
        mRefreshBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (mType) {
                    case TYPE_NO_NETWORK:
                        Intent intent = new Intent(Settings.ACTION_SETTINGS);
                        ((Activity) getContext()).startActivityForResult(intent, Contants.ACTIVITY_CODE_NET_SETTING);
                        break;
                    case TYPE_NO_DATA:
                    case TYPE_SERVER_ERROR:
                        if (mListener != null) {
                            mListener.onRefresh();
                        }
                        break;

                }
            }
        });
    }

    /**
     * 设置无数据类别
     *
     * @param type 类别
     */
    public void setType(int type) {
        mType = type;
        switch (mType) {
            case TYPE_NO_NETWORK:
                mDescTv.setText(getResources().getString(R.string.no_network));
                mIconIv.setImageResource(R.mipmap.nodata_no_network);
                mRefreshBtn.setText(getResources().getString(R.string.nodata_set));
                mRefreshBtn.setVisibility(VISIBLE);
                break;
            case TYPE_SERVER_ERROR:
                mDescTv.setText(getResources().getString(R.string.nodata_server_error));
                mIconIv.setImageResource(R.mipmap.nodata_server_error);
                mRefreshBtn.setText(getResources().getString(R.string.nodata_retry));
                mRefreshBtn.setVisibility(VISIBLE);
                break;
            case TYPE_NO_DATA:
                mDescTv.setText(getResources().getString(R.string.nodata_empty));
                mIconIv.setImageResource(R.mipmap.nodata_empty);
                mRefreshBtn.setVisibility(GONE);
                break;
        }
    }

    public void setClickListener(SwipeRefreshLayout.OnRefreshListener listener) {
        mListener = listener;
    }
}
